from keras import utils, Sequential, layers, activations, optimizers, losses

# sample = "hihello"

x_data = "where are you from ?"
y_data = "I come from China!!!"
# len_x_data=len(x_data)
# len_y_data=len(y_data)
char_set = set(x_data + y_data)
onehot_dim = len(char_set)

int_to_char = {i: j for (i, j) in enumerate(char_set)}
char_to_int = {j: i for (i, j) in enumerate(char_set)}
x_data = [char_to_int[i] for i in x_data]
y_data = [char_to_int[i] for i in y_data]
seq_len = len(x_data)

x_data = utils.to_categorical(x_data).reshape(-1, seq_len, onehot_dim)
y_data = utils.to_categorical(y_data).reshape(-1, seq_len, onehot_dim)
print()

model = Sequential([
    layers.LSTM(units=128, return_sequences=True),
    layers.LSTM(units=128, return_sequences=True),
    layers.Dense(units=onehot_dim, activation=activations.softmax)
])
model.build(input_shape=(None, seq_len, onehot_dim))
model.summary()
model.compile(optimizer=optimizers.Adam(), loss=losses.categorical_crossentropy, metrics='acc')
model.fit(x_data, y_data, batch_size=100, epochs=1000)
res = model.predict_classes(x_data)
print()
print("".join(int_to_char[i] for i in res[0]))
